ScreenRenderPass.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using UnityEngine;
  2. using UnityEngine.Rendering;
  3. using UnityEngine.Rendering.Universal;
  4. #if UNITY_6000_0_OR_NEWER
  5. using System;
  6. using UnityEngine.Rendering.RenderGraphModule;
  7. using UnityEngine.Rendering.RenderGraphModule.Util;
  8. using RenderGraphUtils = UnityEngine.Rendering.RenderGraphModule.Util.RenderGraphUtils;
  9. #endif
  10. public class DustyroomRenderPass : ScriptableRenderPass {
  11. private Material _passMaterial;
  12. private bool _requiresColor;
  13. private bool _isBeforeTransparents;
  14. private PassData _passData;
  15. private ProfilingSampler _profilingSampler;
  16. private RTHandle _copiedColor;
  17. #if UNITY_6000_0_OR_NEWER
  18. private RenderTextureDescriptor _texDescriptor = new(Screen.width, Screen.height,
  19. RenderTextureFormat.Default, 0);
  20. #endif
  21. private const string TexName = "_BlitTexture";
  22. private static readonly int BlitTextureShaderID = Shader.PropertyToID(TexName);
  23. public void Setup(Material mat, bool requiresColor, bool isBeforeTransparents, string featureName,
  24. in RenderingData renderingData) {
  25. _passMaterial = mat;
  26. _requiresColor = requiresColor;
  27. _isBeforeTransparents = isBeforeTransparents;
  28. _profilingSampler ??= new ProfilingSampler(featureName);
  29. var colorCopyDescriptor = renderingData.cameraData.cameraTargetDescriptor;
  30. colorCopyDescriptor.depthBufferBits = (int)DepthBits.None;
  31. #if UNITY_2022_3_OR_NEWER
  32. RenderingUtils.ReAllocateIfNeeded(ref _copiedColor, colorCopyDescriptor, name: "_FullscreenPassColorCopy");
  33. #endif
  34. _passData ??= new PassData();
  35. }
  36. public void Dispose() {
  37. _copiedColor?.Release();
  38. }
  39. #if UNITY_6000_0_OR_NEWER
  40. public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameData) {
  41. UniversalResourceData resourceData = frameData.Get<UniversalResourceData>();
  42. UniversalCameraData cameraData = frameData.Get<UniversalCameraData>();
  43. // The following line ensures that the render pass doesn't blit from the back buffer.
  44. if (resourceData.isActiveTargetBackBuffer) {
  45. return;
  46. }
  47. {
  48. _texDescriptor.width = cameraData.cameraTargetDescriptor.width;
  49. _texDescriptor.height = cameraData.cameraTargetDescriptor.height;
  50. _texDescriptor.depthBufferBits = 0;
  51. }
  52. TextureHandle srcCamColor = resourceData.activeColorTexture;
  53. TextureHandle dst = UniversalRenderer.CreateRenderGraphTexture(renderGraph, _texDescriptor, TexName, false);
  54. // This check is to avoid an error from the material preview in the scene.
  55. if (!srcCamColor.IsValid() || !dst.IsValid()) {
  56. return;
  57. }
  58. RenderGraphUtils.BlitMaterialParameters blit1 = new(srcCamColor, dst, _passMaterial, 0);
  59. renderGraph.AddBlitPass(blit1, $"{_profilingSampler.name} (Effect Pass)");
  60. renderGraph.AddCopyPass(dst, srcCamColor);
  61. }
  62. [Obsolete("This rendering path is for compatibility mode only (when Render Graph is disabled).", false)]
  63. #endif
  64. public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData) {
  65. _passData.effectMaterial = _passMaterial;
  66. _passData.requiresColor = _requiresColor;
  67. _passData.isBeforeTransparents = _isBeforeTransparents;
  68. _passData.profilingSampler = _profilingSampler;
  69. _passData.copiedColor = _copiedColor;
  70. ExecutePass(_passData, ref renderingData, ref context);
  71. }
  72. private static void ExecutePass(PassData passData, ref RenderingData renderingData,
  73. ref ScriptableRenderContext context) {
  74. var passMaterial = passData.effectMaterial;
  75. var requiresColor = passData.requiresColor;
  76. var copiedColor = passData.copiedColor;
  77. var profilingSampler = passData.profilingSampler;
  78. if (passMaterial == null) {
  79. return;
  80. }
  81. if (renderingData.cameraData.isPreviewCamera) {
  82. return;
  83. }
  84. #if UNITY_2022_3_OR_NEWER
  85. CommandBuffer cmd = renderingData.commandBuffer;
  86. #else
  87. CommandBuffer cmd = CommandBufferPool.Get();
  88. #endif
  89. var cameraData = renderingData.cameraData;
  90. using (new ProfilingScope(cmd, profilingSampler)) {
  91. if (requiresColor) {
  92. #if UNITY_2022_3_OR_NEWER
  93. var source = passData.isBeforeTransparents
  94. ? cameraData.renderer.GetCameraColorBackBuffer(cmd)
  95. : cameraData.renderer.cameraColorTargetHandle;
  96. Blitter.BlitCameraTexture(cmd, source, copiedColor);
  97. #else
  98. var source = cameraData.renderer.cameraColorTarget;
  99. cmd.Blit(source, copiedColor);
  100. #endif
  101. passMaterial.SetTexture(BlitTextureShaderID, copiedColor);
  102. }
  103. #if UNITY_2022_3_OR_NEWER
  104. CoreUtils.SetRenderTarget(cmd, cameraData.renderer.GetCameraColorBackBuffer(cmd));
  105. #else
  106. CoreUtils.SetRenderTarget(cmd, cameraData.renderer.cameraColorTarget);
  107. #endif
  108. CoreUtils.DrawFullScreen(cmd, passMaterial);
  109. context.ExecuteCommandBuffer(cmd);
  110. cmd.Clear();
  111. }
  112. }
  113. private class PassData {
  114. internal Material effectMaterial;
  115. internal bool requiresColor;
  116. internal bool isBeforeTransparents;
  117. public ProfilingSampler profilingSampler;
  118. public RTHandle copiedColor;
  119. }
  120. }